package com.hongdee.insurance.statstics.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.StringUtil;
import com.hongdee.insurance.Constant;
import com.hongdee.insurance.order.service.ITInsuranceOrderStatsticsService;
import com.hongdee.insurance.task.service.ITInsuranceOrderStatsticsTaskService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王帅
 * @className ${CLASS_NAME}
 * @type
 * @description
 * @path com.hongdee.insurance.statstics.controller
 * @create 2017/7/3 16:42
 **/
@Controller
@RequestMapping("/insurance/statstics")
public class TInsuranceOrderStatsticsController {

    @Autowired
    private ITInsuranceOrderStatsticsService orderStatsticsService;
    @Autowired
    private ITInsuranceOrderStatsticsTaskService orderStatsticsTaskService;


    /**
     * 当天统计并分页
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/day/page")
    @ResponseBody
    public Map dayPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "start", defaultValue = "") String start,
                       @RequestParam(name = "limit", defaultValue = "") String limit,
                       @RequestBody Map data) throws Exception {
        Map map=orderStatsticsService.dayAndWeek(uid, Constant.STATSTICS_TYPE.day.name(), StringUtil.formatEmpty(data.get("time")),
                StringUtil.formatEmpty(data.get("brand")),StringUtil.formatEmpty(data.get("companyId")),
                StringUtil.formatEmpty(data.get("dict")),start,limit);
        map.put("success",true);
        map.put("msg","查询成功!!");
        return map;
    }

    /**
     * 当天统计导出Excel
     * @param uid
     * @param token
     * @param request
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/day/exp/execl")
    @ResponseBody
    public ResultEntity dayExpExecl(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       HttpServletRequest request,
                       @RequestBody Map data) throws Exception {
        XSSFWorkbook wb = orderStatsticsService.dayAndWeekExpExcel(uid, Constant.STATSTICS_TYPE.day.name(), StringUtil.formatEmpty(data.get("time")),
                StringUtil.formatEmpty(data.get("brand")),StringUtil.formatEmpty(data.get("companyId")),
                StringUtil.formatEmpty(data.get("dict")));
        return Constant.expExcel(request,wb);
    }


    /**
     * 当周统计并分页
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/week/page")
    @ResponseBody
    public Map weekPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "start", defaultValue = "") String start,
                       @RequestParam(name = "limit", defaultValue = "") String limit,
                       @RequestBody Map data) throws Exception {
        Map map=orderStatsticsService.dayAndWeek(uid,Constant.STATSTICS_TYPE.week.name(), StringUtil.formatEmpty(data.get("time")),
                StringUtil.formatEmpty(data.get("brand")),StringUtil.formatEmpty(data.get("companyId")),
                StringUtil.formatEmpty(data.get("dict")),start,limit);
        map.put("success",true);
        map.put("msg","查询成功!!");
        return map;
    }

    /**
     * 当周统计导出Excel
     * @param uid
     * @param token
     * @param request
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/week/exp/execl")
    @ResponseBody
    public ResultEntity weekExpExecl(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    HttpServletRequest request,
                                    @RequestBody Map data) throws Exception {
        XSSFWorkbook wb = orderStatsticsService.dayAndWeekExpExcel(uid, Constant.STATSTICS_TYPE.week.name(), StringUtil.formatEmpty(data.get("time")),
                StringUtil.formatEmpty(data.get("brand")),StringUtil.formatEmpty(data.get("companyId")),
                StringUtil.formatEmpty(data.get("dict")));
        return Constant.expExcel(request,wb);
    }

    /**
     * 当周保费分配统计
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     */
    @RequestMapping("/week/indurance/page")
    @ResponseBody
    public Map weekInsurancePage(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestParam(name = "start", defaultValue = "") String start,
                                @RequestParam(name = "limit", defaultValue = "") String limit,
                                @RequestBody Map data) throws Exception {
        Map map=new HashMap();
        map.put("success",true);
        map=orderStatsticsService.weekInsurancePage(uid,StringUtil.formatEmpty(data.get("time")),
                StringUtil.formatEmpty(data.get("companyId")),StringUtil.formatEmpty(data.get("insuranceType")),
                StringUtil.formatEmpty(data.get("dict")),StringUtil.formatEmpty(data.get("brand")),start,limit);
        map.put("msg","查询成功!!");
        return map;
    }

    /**
     * 当周保费分配导出Excel
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param request
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/week/indurance/exp/excel")
    @ResponseBody
    public ResultEntity weekInsuranceExpExcel(@RequestParam(name = "uid", defaultValue = "") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestParam(name = "start", defaultValue = "") String start,
                                 @RequestParam(name = "limit", defaultValue = "") String limit,
                                 HttpServletRequest request,
                                 @RequestBody Map data) throws Exception {
        XSSFWorkbook wb=orderStatsticsService.weekAndMonthInsuranceExpExcel(uid,StringUtil.formatEmpty(data.get("time")),
                StringUtil.formatEmpty(data.get("companyId")),StringUtil.formatEmpty(data.get("insuranceType")),
                StringUtil.formatEmpty(data.get("dict")),StringUtil.formatEmpty(data.get("brand")),Constant.STATSTICS_TYPE.week.name());
        return Constant.expExcel(request,wb);
    }

    /**
     * 当月保费分配统计
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/month/indurance/page")
    @ResponseBody
    public Map monthInsurancePage(@RequestParam(name = "uid", defaultValue = "") String uid,
                                  @RequestParam(name = "token", defaultValue = "") String token,
                                  @RequestParam(name = "start", defaultValue = "") String start,
                                  @RequestParam(name = "limit", defaultValue = "") String limit,
                                  @RequestBody Map data) throws Exception {
        Map map=new HashMap();
        map.put("success",true);
        map=orderStatsticsService.monthInsurancePage(uid,StringUtil.formatEmpty(data.get("time")),
                StringUtil.formatEmpty(data.get("companyId")),StringUtil.formatEmpty(data.get("insuranceType")),
                StringUtil.formatEmpty(data.get("dict")),StringUtil.formatEmpty(data.get("brand")),start,limit);
        map.put("msg","查询成功!!");
        return map;
    }

    /**
     * 月保费分配统计导出Excel
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param request
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/month/indurance/exp/excel")
    @ResponseBody
    public ResultEntity monthInsuranceExpExcel(@RequestParam(name = "uid", defaultValue = "") String uid,
                                              @RequestParam(name = "token", defaultValue = "") String token,
                                              @RequestParam(name = "start", defaultValue = "") String start,
                                              @RequestParam(name = "limit", defaultValue = "") String limit,
                                              HttpServletRequest request,
                                              @RequestBody Map data) throws Exception {
        XSSFWorkbook wb=orderStatsticsService.weekAndMonthInsuranceExpExcel(uid,StringUtil.formatEmpty(data.get("time")),
                StringUtil.formatEmpty(data.get("companyId")),StringUtil.formatEmpty(data.get("insuranceType")),
                StringUtil.formatEmpty(data.get("dict")),StringUtil.formatEmpty(data.get("brand")),Constant.STATSTICS_TYPE.month.name());
        return Constant.expExcel(request,wb);
    }

    /**
     * 月统计
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     */
    @RequestMapping("/month/indurance/statstics")
    @ResponseBody
    public Map monthInsuranceStatstics(@RequestParam(name = "uid", defaultValue = "") String uid,
                                       @RequestParam(name = "token", defaultValue = "") String token,
                                       @RequestParam(name = "start", defaultValue = "") String start,
                                       @RequestParam(name = "limit", defaultValue = "") String limit,
                                       @RequestBody Map data) throws Exception {
        Map map=this.orderStatsticsService.monthInsuranceStatics(uid,StringUtil.formatEmpty(data.get("companyId")),
                StringUtil.formatEmpty(data.get("time")),StringUtil.formatEmpty(data.get("brand")),
                StringUtil.formatEmpty(data.get("dict")),start,limit);
        map.put("success",true);
        map.put("msg","查询成功!!");
        return map;
    }

    @RequestMapping("/month/indurance/statstics/exp/excel")
    @ResponseBody
    public ResultEntity monthInsuranceStatsticsExpExcel(@RequestParam(name = "uid", defaultValue = "") String uid,
                                                @RequestParam(name = "token", defaultValue = "") String token,
                                                @RequestParam(name = "start", defaultValue = "") String start,
                                                @RequestParam(name = "limit", defaultValue = "") String limit,
                                                HttpServletRequest request,
                                                @RequestBody Map data) throws Exception {
        XSSFWorkbook wb=orderStatsticsService.monthInsuranceStaticsExpExcel(uid,StringUtil.formatEmpty(data.get("companyId")),
                StringUtil.formatEmpty(data.get("time")),StringUtil.formatEmpty(data.get("brand")),
                StringUtil.formatEmpty(data.get("dict")));
        return Constant.expExcel(request,wb);
    }

    /**
     * 返利台账统计
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     */
    @RequestMapping("/rebate")
    @ResponseBody
    public Map insuranceRebate(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "start", defaultValue = "") String start,
                               @RequestParam(name = "limit", defaultValue = "") String limit,
                               @RequestBody Map data) throws Exception {
        List<Map<String,Object>> list=this.orderStatsticsService.statsticsMonthRebate(uid,StringUtil.formatEmpty(data.get("companyId")),
                StringUtil.formatEmpty(data.get("date")));
        Map map=new HashMap();
        map.put("dataList",list);
        map.put("success",true);
        map.put("msg","查询成功");
        return map;
    }

    /**
     * 返利导出Excel
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param request
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/rebate/exp/excel")
    @ResponseBody
    public ResultEntity insuranceRebateExpExcel(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token,
                               @RequestParam(name = "start", defaultValue = "") String start,
                               @RequestParam(name = "limit", defaultValue = "") String limit,
                               HttpServletRequest request,
                               @RequestBody Map data) throws Exception {
        XSSFWorkbook xb=this.orderStatsticsService.insruanceRebatExpExcel(uid,StringUtil.formatEmpty(data.get("time")),
                StringUtil.formatEmpty(data.get("companyId")));
        return Constant.expExcel(request,xb);
    }

    /**
     * 送修比
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     */
    @RequestMapping("/repair")
    @ResponseBody
    public Map insuranceRepairPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                                   @RequestParam(name = "token", defaultValue = "") String token,
                                   @RequestParam(name = "start", defaultValue = "") String start,
                                   @RequestParam(name = "limit", defaultValue = "") String limit,
                                   @RequestBody Map data) throws Exception {
        Map map=this.orderStatsticsService.insuranceRepairPage(uid,StringUtil.formatEmpty(data.get("time")),
                StringUtil.formatEmpty(data.get("companyId")),start,limit);
        map.put("success",true);
        map.put("msg","查询成功!!");
        return map;
    }

    /**
     * 单店保险销售报表
     * @param uid
     * @param token
     * @param data
     * @return
     */
    @RequestMapping("/sales")
    @ResponseBody
    public Map insuranceSales(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestBody Map data) throws Exception {
        Map map=new HashMap();
        List<Map<String,Object>> list=this.orderStatsticsService.insuranceSales(StringUtil.formatEmpty(data.get("companyId")),StringUtil.formatEmpty(data.get("time")),
                StringUtil.formatEmpty(data.get("insuranceType")),uid);
        map.put("success",true);
        map.put("dataList",list);
        map.put("msg","查询成功");
        return map;
    }


    /**
     * 保险月报
     * @param uid
     * @param token
     * @param data
     * @return
     */
    @RequestMapping("/month")
    @ResponseBody
    public Map monthStatstics(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestBody Map data) throws Exception {
        Map map=new HashMap();
        List<Map<String,Object>> list=this.orderStatsticsService.monthStatics(StringUtil.formatEmpty(data.get("companyId")),
                StringUtil.formatEmpty(data.get("time")),StringUtil.formatEmpty(data.get("insureType")));
        map.put("success",true);
        map.put("dataList",list);
        map.put("msg","查询成功");
        return map;
    }

    /**
     * 保险返利台账
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/month/rebate")
    @ResponseBody
    public Map statsticsMonthRebate(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestBody Map data) throws Exception {
        List<Map<String,Object>> list=this.orderStatsticsService.statsticsMonthRebate(uid,StringUtil.formatEmpty(data.get("companyId")),
                StringUtil.formatEmpty(data.get("time")));
        Map map=new HashMap();
        map.put("dataList",list);
        map.put("success",true);
        map.put("msg","查询成功");
        return map;
    }
}
